<?php
/*
 * Klasa posredniczaca w dostepie do smarty, obsluguje kontrole cacheowania
 */
class TemplateMaster extends Smarty {
	private $clearCache = false;

	public function CacheControl($tpl, $param='') {

		$this->force_compile = 0;
		if($this->caching!=2) {
			$this->cache_lifetime = 0;
			//$this->force_compile = true;
			//	    echo"aaa";
			return false;
		}

		// $param =
		// array ( 'cache_id' => [string],
		// 'compile_id'=> [string],
		// 'lifetime' => [integer],
		// 'write' => [boolean] );
		$_cache_file = null;
		$_auto_id = $this->_get_auto_id($param['cache_id'], 1);
		$_cache_file = $this->_get_auto_filename($this->cache_dir, $tpl, $_auto_id);
		if($this->template_exists($tpl)) {
		//$_cache_file = $this->_get_auto_filename($this->cache_dir, $tpl, $_auto_id);
		//$_compile_file = $this->_get_auto_filename($this->compile_dir, $tpl, $_auto_id);

			if(!$this->_is_compiled($tpl, $this->compile_dir) && is_file($_cache_file)) {
				$this->clear_cache($tpl, $param['cache_id'], $param['cache_id']);
			}
		}

		//$memcacheCheck = false;
		//if(Enviroment::CheckMemcache()) {
		//	$memcacheCheck = MfMemcache::exists('cacheLock'.$param['cache_id']);
		//}

		$this->cache_lifetime = -1;

		// sprawdzam cache
		if (!$this->is_cached($tpl, $param['cache_id'])) {
			return false;

		} elseif ((isset($param['methodRun']) && Registry::Get('methodRun') == true  && (isset($param['write']) || $this->caching!=2 )) || (isset($param['write']) || $this->caching!=2 && !isset($param['methodRun']))) {
			MFLog::Debug("writing:".$tpl);
			//$this->clear_cache($tpl, $param['cache_id']);
			$this->clearCache = true;

			if(is_file($_cache_file)) {
			///$futureTime = time();
			///touch($_cache_file, $futureTime, $futureTime);
			//echo $futureTime."<br>";
			//$fh = fopen($_cache_file, 'w');

				$_contents = $this->_read_file($_cache_file);
				$_info_start = strpos($_contents, "\n") + 1;
				$_info_len = (int)substr($_contents, 0, $_info_start - 1);
				$_cache_info = unserialize(substr($_contents, $_info_start, $_info_len));
				$results = substr($_contents, $_info_start + $_info_len);

				$cacheInfo = $this->_cache_info;
				$cacheInfo['timestamp'] = time();
				//$cacheInfo['mexyk'] = time();
				//$cacheInfo['expires'] = '0';
				
				$sCacheInfo = serialize($cacheInfo);
				$header = strlen($sCacheInfo) . "\n" .$sCacheInfo;

				$fh = fopen($_cache_file, 'w');

				fwrite($fh, $header.$results);
				fclose($fh);

			}
			//if(Enviroment::CheckMemcache()) {
			//	MfMemcache::set('cacheLock'.$param['cache_id'], true);
			//}
			// force overwrite cache file
			//$this->force_compile = true;
			//$this->cache_lifetime = 0;
			return true;

		} elseif (isset($param['lifetime'])) {
		//if(!Core::GetAppSafeMode() && $memcacheCheck == false) {
			if(!Core::GetAppSafeMode()) {
			// sprawdza czas cache
				MFLog::Debug('check:'.date("d H:i:s", $this->_cache_info['timestamp']));
				return ($this->_cache_info['timestamp'] < time() - $param['lifetime']) ? false : true;
			} else {
				return true;
			}

		} else {
			return true;
		}
	}

	public function fetch($resource_name, $cache_id = null, $compile_id = null, $display = false) {
	//if(Enviroment::CheckMemcache()) {
	//	MfMemcache::remove('cacheLock'.$cache_id);
	//}

		if($this->clearCache == true) {
			$_auto_id = $this->_get_auto_id($cache_id, 1);
			$_cache_file = $this->_get_auto_filename($this->cache_dir, $resource_name, $_auto_id);

			if(is_file($_cache_file)) {

			unlink($_cache_file);

				$this->force_compile = true;
				$this->is_cached($resource_name, $cache_id);
				$this->force_compile = false;

			}
		}
		$this->clearCache = false;
		return parent::fetch($resource_name, $cache_id, $compile_id, $display);
	}



}

